package cn.elwy.rcp.ui.wizard;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

import cn.elwy.common.util.AssertUtil;
import cn.elwy.rcp.core.model.ui.DialogInfo;
import cn.elwy.rcp.core.model.ui.DialogType;
import cn.elwy.rcp.ui.config.DialogConfigFactory;
import cn.elwy.rcp.ui.config.DialogConfigListener;

/**
 * 自定义向导对话框的基类，可实现自定义按钮动作
 * @author huangsq
 * @version 1.0, 2013-11-20
 * @since 1.0, 2013-11-20
 */
public class BaseWizardDialog extends WizardDialog {

  private String dialogId;
  private DialogInfo dialogInfo;
  private String helpContextId;

  public BaseWizardDialog(Shell parentShell, BaseWizard wizard) {
    this(parentShell, wizard, wizard.getDialogId());
  }

  /**
   * @wbp.parser.constructor
   */
  public BaseWizardDialog(Shell parentShell, BaseWizard wizard, String dialogId) {
    super(parentShell, wizard);
    setDialogId(dialogId);
  }

  @Override
  public int open() {
    init();
    int open = super.open();
    save();
    return open;
  }

  protected void init() {
    Wizard wizard = (Wizard) getWizard();
    wizard.setForcePreviousAndNextButtons(true);
    if (AssertUtil.isNotEmpty(helpContextId)) {
      wizard.setHelpAvailable(true);
      PlatformUI.getWorkbench().getHelpSystem().setHelp(getShell(), helpContextId);
    }
    DialogInfo dialogInfo = getDialogInfo();

    create();
    getShell().setSize(dialogInfo.getWidth(), dialogInfo.getHeight());
    getShell().setLocation(getLocation());
    getShell().setMinimumSize(dialogInfo.getMinimumWidth(), dialogInfo.getMinimumHeight());
    getShell().setMaximized(dialogInfo.isMaximized());

    DialogConfigListener dialogConfigListener = new DialogConfigListener();
    dialogConfigListener.setDialogInfo(dialogInfo);
    getShell().addControlListener(dialogConfigListener);
  }

  protected void save() {
    if (getDialogInfo().isCanSave()) {
      DialogConfigFactory.saveDialogInfo(dialogInfo);
    }
  }

  /** 设置窗口居中 */
  protected Point getLocation() {
    if (!getDialogInfo().isCenter()) {
      return new Point(dialogInfo.getX(), dialogInfo.getY());
    }
    Rectangle shellBounds = null;
    if (getParentShell() == null) {
      shellBounds = Display.getCurrent().getBounds();
    } else {
      shellBounds = getParentShell().getBounds();
    }
    Point dialogSize = getShell().getSize();
    return new Point(shellBounds.x + (shellBounds.width - dialogSize.x) / 2,
        shellBounds.y + (shellBounds.height - dialogSize.y) / 2);
  }

  @Override
  protected int getShellStyle() {
    return super.getShellStyle() | getDialogInfo().getStyle();
  }

  @Override
  protected void buttonPressed(int buttonId) {
    boolean result = true;
    IWizardPage currentPage = getWizard().getContainer().getCurrentPage();
    if (currentPage instanceof BaseWizardPage) {
      result = buttonPressed(buttonId, (BaseWizardPage) currentPage);
    }
    if (result) {
      super.buttonPressed(buttonId);
    }
  }

  /**
   * 自定义向导的按钮事件
   * @param buttonId
   * @param currentPage
   */
  private boolean buttonPressed(int buttonId, BaseWizardPage currentPage) {
    switch (buttonId) {
      case IDialogConstants.BACK_ID: // 点击backButton
        return backButtonPressed(currentPage);
      case IDialogConstants.FINISH_ID: // 点击finishButton
        return finishButtonPressed(currentPage);
      case IDialogConstants.NEXT_ID: // 点击nextButton
        return nextButtonPressed(currentPage);
      default:
        return true;
    }
  }

  @Override
  public boolean close() {
    boolean result = true;
    IWizardPage currentPage = getWizard().getContainer().getCurrentPage();
    if (currentPage instanceof BaseWizardPage) {
      result = closePressed((BaseWizardPage) currentPage);
    }
    if (result) {
      return super.close();
    }
    return false;
  }

  private boolean backButtonPressed(BaseWizardPage currentPage) {
    return currentPage.backButtonPressed();
  }

  private boolean closePressed(BaseWizardPage currentPage) {
    return currentPage.closePressed();
  }

  private boolean finishButtonPressed(BaseWizardPage currentPage) {
    return currentPage.finishButtonPressed();
  }

  private boolean nextButtonPressed(BaseWizardPage currentPage) {
    return currentPage.nextButtonPressed();
  }

  public String getHelpContextId() {
    return helpContextId;
  }

  public void setHelpContextId(String helpContextId) {
    this.helpContextId = helpContextId;
  }

  public String getDialogId() {
    return dialogId;
  }

  public void setDialogId(String dialogId) {
    this.dialogId = dialogId;
  }

  public synchronized DialogInfo getDialogInfo() {
    if (dialogInfo == null) {
      dialogInfo = DialogConfigFactory.getDialogInfo(dialogId, DialogType.WIZARD);
    }
    return dialogInfo;
  }

  public void setDialogInfo(DialogInfo dialogInfo) {
    this.dialogInfo = dialogInfo;
  }

}
